package com.tinem.platform.service.security.api;

import com.baidu.fsg.uid.UidGenerator;
import com.tinem.platform.module.commons.util.EnvUtil;
import com.tinem.platform.module.pojo.co.RedisKeyEnum;
import com.tinem.platform.module.pojo.vo.security.verification.code.GenerateReq;
import com.tinem.platform.module.pojo.vo.security.verification.code.GenerateRes;
import com.tinem.platform.module.pojo.vo.security.verification.code.VerificationReq;
import com.tinem.platform.module.pojo.vo.security.verification.code.VerificationRes;
import com.tinem.platform.service.security.observable.GenerateObservable;
import com.tinem.platform.service.security.service.VerificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className CodeApi
 * @description TODO
 * @site 
 * @company 
 * @create 2020-09-28 11:33
 */
@Controller
@RequestMapping("/verification/code")
public class VerificationCodeApi {

    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private UidGenerator uidGenerator;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Resource
    VerificationService verificationService;

    @Resource
    EnvUtil envUtil;

    /**
     * 图片验证码生成
     * @param generateReq
     * @return
     */
    @PostMapping("/image/generate.api")
    @ResponseBody
    public GenerateRes generate(@RequestBody GenerateReq generateReq){
        GenerateObservable observable = new GenerateObservable(generateReq,uidGenerator.getUID(),"image");
        applicationContext.publishEvent(observable);
        redisTemplate.opsForValue().set(
                RedisKeyEnum.security_verification_code.getKey(String.valueOf(observable.getId()))
                ,observable.getCode()
                ,observable.getCodeEffectiveTime()
        );

        if (envUtil.isDev() || envUtil.isTest()) {
            // 测试和开发环境将code返回
            return new GenerateRes(observable.getId(),observable.getCode(),observable.getImageBase64(),observable.getCodeEffectiveTime().toMillis());
        }
        return new GenerateRes(observable.getId(),null,observable.getImageBase64(),observable.getCodeEffectiveTime().toMillis());
    }

    /**
     * 短信 & 邮件验证码生成
     * @param channel
     * @param generateReq
     * @return
     */
    @PostMapping("/{channel}/generate.api")
    @ResponseBody
    public GenerateRes generate(@PathVariable("channel")String channel,@RequestBody GenerateReq generateReq){
        GenerateObservable observable = new GenerateObservable(generateReq,uidGenerator.getUID(),channel);
        applicationContext.publishEvent(observable);
        redisTemplate.opsForValue().set(
                RedisKeyEnum.security_verification_code.getKey(observable.getSource().getTo(),String.valueOf(observable.getId()))
                ,observable.getCode()
                ,observable.getCodeEffectiveTime()
        );

        if (envUtil.isDev() || envUtil.isTest()) {
            return new GenerateRes(observable.getId(),observable.getCode(),observable.getImageBase64(),observable.getCodeEffectiveTime().toMillis());
        }
        return new GenerateRes(observable.getId(),null,observable.getImageBase64(),observable.getCodeEffectiveTime().toMillis());
    }

    @PostMapping("verification.api")
    @ResponseBody
    public VerificationRes verification(@RequestBody VerificationReq verificationReq){


        String codeId = String.valueOf(verificationReq.getCodeId());
        String code = verificationReq.getCode();
        String account = verificationReq.getAccount();

        // 若账号为空，则认为是图片校验
        return verificationService.verification(account,codeId,code);
    }

}
